package es.ibm.santillana.mdm.entryPreview.clonation;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.json.java.JSONArray;
import com.ibm.json.java.OrderedJSONObject;
import com.ibm.pim.catalog.Catalog;
import com.ibm.pim.catalog.CatalogManager;
import com.ibm.pim.catalog.item.Item;
import com.ibm.pim.context.Context;
import com.ibm.pim.context.PIMContextFactory;
import com.ibm.pim.lookuptable.LookupTable;
import com.ibm.pim.lookuptable.LookupTableEntry;
import com.ibm.pim.lookuptable.LookupTableManager;
import com.ibm.pim.utils.Logger;
import com.ibm.pim.web.framework.dispatcher.AsyncEnabled;
import com.ibm.pim.web.framework.dispatcher.RequestResponseEnabled;

import es.ibm.santillana.mdm.clonation.ClonationManagement;
import es.ibm.santillana.mdm.common.bussinesmodel.ConfigModel;
import es.ibm.santillana.mdm.common.util.ConstSantillana;
import es.ibm.santillana.mdm.common.util.SantillanaLogger;

public class CloneAction implements AsyncEnabled,RequestResponseEnabled{ 

	
	HttpServletRequest request;
	HttpServletResponse response;
	
	//send an json array for the collboration area  
	String responseContent ="";
	
	/* (sin Javadoc) 
	 * @see com.ibm.pim.web.framework.dispatcher.AsyncEnabled#getContentType()
	 */
	public String getContentType() {
		
		return JSON;
	}

	/* (sin Javadoc)
	 * @see com.ibm.pim.web.framework.dispatcher.AsyncEnabled#getResponseContent()
	 */
	public String getResponseContent() {
		
		return responseContent;
	}

	/* (sin Javadoc)
	 * @see com.ibm.pim.web.framework.dispatcher.RequestResponseEnabled#setRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setRequest(HttpServletRequest request) {
		
		this.request = request;
		
	}

	/* (sin Javadoc)
	 * @see com.ibm.pim.web.framework.dispatcher.RequestResponseEnabled#setResponse(javax.servlet.http.HttpServletResponse)
	 */
	public void setResponse(HttpServletResponse response) {
		this.response = response;
		
	}
	
	
	public void cloneItem() throws IOException, SQLException, Exception{
		
		JSONArray aListOrden = new JSONArray();
		Logger log = SantillanaLogger.getLogger();
		log.logInfo("EN ACTION CLONADO");
		String primaryKey = request.getParameter("sPK");
		String bMulti = request.getParameter("bMulti");
		String bPack = request.getParameter("bPack");
		String sSociedades = request.getParameter("sSociedades");
		String[] aSociedades = sSociedades.split(";");
		
		log.logInfo("primaryKey "+primaryKey);
		log.logInfo("Multi "+bMulti);
		log.logInfo("bPack "+bPack);
		OrderedJSONObject urlJSON = new OrderedJSONObject();
		Context ctx = PIMContextFactory.getCurrentContext();
		
		String country = ConfigModel.getCountryCodeFromUser(ctx.getCurrentUser());
		log.logInfo("country "+country);
		LookupTableManager lkpMan = ctx.getLookupTableManager();
		LookupTable lkp = lkpMan.getLookupTable("System Parameter LKP");
		LookupTableEntry itmLkpUser = lkp.getLookupTableEntry("user.admin.name");
		LookupTableEntry itmLkpPwd = lkp.getLookupTableEntry("user.admin.pwd");
		LookupTableEntry itmLkpCmp = lkp.getLookupTableEntry("company");
		
		String user = (String) itmLkpUser.getAttributeValue("Code_Value_LTS/Value");
		String pwd = (String) itmLkpPwd.getAttributeValue("Code_Value_LTS/Value");
		String cmp = (String) itmLkpCmp.getAttributeValue("Code_Value_LTS/Value");
		
		
		ctx = PIMContextFactory.getContext(user, pwd, cmp);
		CatalogManager caMan = ctx.getCatalogManager();
		String url = "";
		Catalog ctg = caMan.getCatalog(ConstSantillana.MAESTRO_ARTICULOS);
		if (bMulti.equals("true")){
			String[] pkList;
			pkList = primaryKey.split(";");
			for(int i=0;i<pkList.length;i++){
				String pk = pkList[i];
				Item itm = ctg.getItemByPrimaryKey(pk);
				url = ClonationManagement.cloneItem(ctx, itm, bPack, country, aSociedades);
			}
		}else{
			Item itm = ctg.getItemByPrimaryKey(primaryKey);
			url = ClonationManagement.cloneItem(ctx, itm, bPack, country, aSociedades);
		}
		urlJSON.put("url", url);
		aListOrden.add(urlJSON);
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		responseContent = aListOrden.serialize(true);
		
	}

	

}
